from django.db import models
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField  # 需要上传图片的使用这个库

# Create your models here.

# 分类表
class Category(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField('分类名称',max_length=20)
    
    class Meta:
        db_table = 'category'
        verbose_name = '分类管理'
        verbose_name_plural = '分类管理'
        
    def __str__(self):
        return self.name
    
# 新闻表
class News(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField('标题',max_length=100)
    # content = models.TextField('新闻内容')
    content = RichTextUploadingField()
    # blank=True,null=True 表示非必须，不填时默认为null
    cover = models.ImageField('图片',upload_to='news',blank=True,null=True,help_text='最佳尺寸：480*270')
    # auto_now_add=True,editable=True 自动增加且可编辑
    create_time = models.DateTimeField('创建日期',auto_now_add=True,editable=True)
    update_time = models.DateTimeField('更新日期',auto_now=True,editable=True)
    category = models.ForeignKey(Category,on_delete=models.CASCADE,verbose_name='分类')
    
    class Meta:
        db_table = 'news'
        verbose_name = '新闻管理'
        verbose_name_plural = '新闻管理'
        
    def __str__(self):
        return self.title

    